package com.reremouse.rerebbs.web.mb;

import com.reremouse.rerebbs.dao.IBbsTalkDAO;
import com.reremouse.rerebbs.dao.IBbsThreadDAO;
import com.reremouse.rerebbs.dao.IBbsUserDAO;
import com.reremouse.rerebbs.dao.IBbsZoneDAO;
import com.reremouse.rerebbs.model.*;
import com.reremouse.util.JsfHelper;
import com.reremouse.util.SpringHelper;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

@ManagedBean
@ViewScoped
public class AddNewThread
        implements Serializable {
    BbsZone zone = null;
    BbsTalk talk = new BbsTalk();
    BbsThread thread = new BbsThread();
    IBbsUserDAO userDAO = (IBbsUserDAO) SpringHelper.getSpringBean("BbsUserDAO");
    IBbsThreadDAO bbsThreadDAO = (IBbsThreadDAO) SpringHelper.getSpringBean("BbsThreadDAO");
    IBbsTalkDAO bbsTalkDAO = (IBbsTalkDAO) SpringHelper.getSpringBean("BbsTalkDAO");
    IBbsZoneDAO bbsZoneDAO = (IBbsZoneDAO) SpringHelper.getSpringBean("BbsZoneDAO");
    @ManagedProperty("#{clientSession}")
    ClientSession cs;

    public BbsZone getZone() {
        return this.zone;
    }

    public void setZone(BbsZone zone) {
        this.zone = zone;
    }

    public BbsTalk getTalk() {
        return this.talk;
    }

    public void setTalk(BbsTalk talk) {
        this.talk = talk;
    }

    public BbsThread getThread() {
        return this.thread;
    }

    public void setThread(BbsThread thread) {
        this.thread = thread;
    }

    public ClientSession getCs() {
        return this.cs;
    }

    public void setCs(ClientSession cs) {
        this.cs = cs;
    }

    @PostConstruct
    public void init() {
        HttpServletRequest request = JsfHelper.getRequest();
        String idt = request.getParameter("zone_id");
        if (idt != null) {
            this.zone = this.bbsZoneDAO.findById(Long.valueOf(Long.parseLong(idt)));
        }
    }

    public String add_ok() {
        HttpServletRequest request = JsfHelper.getRequest();
        FacesContext fc = FacesContext.getCurrentInstance();
        BbsUser bu = this.cs.getUsr();
        bu.setScore(bu.getScore() + BBSScoreRegular.NEW_THREAD);
        this.userDAO.updateBbsUser(bu);

        this.thread.setGenBy(bu);
        this.thread.setGenTime(new Date());
        this.thread.setZone(this.zone);
        this.thread.setReadCount(Integer.valueOf(0));
        this.talk.setGenBy(this.cs.getUsr());
        this.talk.setGenTime(new Date());
        this.talk.setLastEditTime(new Date());
        this.talk.setThread(this.thread);
        this.talk.setIp(request.getRemoteAddr());

        this.thread.setLatestTalk(this.talk);
        this.bbsThreadDAO.save(this.thread);


        HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
        try {
            response.sendRedirect("ThreadList.jspx?id=" + this.zone.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        fc.responseComplete();
        return null;
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.AddNewThread

 * JD-Core Version:    0.7.0.1

 */